package com.example.lists;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ListToMap {
    public static void main(String[] args) {
        List<User> list = new ArrayList<User>();
        list.add(new User(1, 1, "11"));
        list.add(new User(2, 2, "11"));
        list.add(new User(3, 3, "33"));
        list.add(new User(5, 5, "55"));
        list.add(new User(5, null, "66"));
        Map<String, List<User>> map = list.stream().collect(Collectors.groupingBy(User::getLable));
        System.out.println(map);
        map.forEach((lable, users) -> {
            System.out.println(lable + users);
        });
        //遍历map推荐使用entrySet，速度比keySet快
        //entrySet
        Iterator<Map.Entry<String, List<User>>> iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, List<User>> entry = iter.next();
            Object key = entry.getKey();
            Object val = entry.getValue();
        }
        Map<String, Integer> mm = list.stream().collect(Collectors.toMap(User::getLable, User::getParentId,
                (key1, key2) -> key1));
        System.out.println(mm);
        //keySet
        /*Map map = new HashMap();
        Iterator iter = map.keySet().iterator();
        while (iter.hasNext()) {
            Object key = iter.next();
            Object val = map.get(key);
        }*/
    }
}
